package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 获取 user_id
        Long user_id = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(user_id);

        // 先检查是否这个用户的购物车已经有这个菜品
        ShoppingCart shoppingCart1 = shopCartMapper.getIsExist(shoppingCart);
        if (shoppingCart1 != null) {
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shopCartMapper.updateNumber(shoppingCart1);
        }else {
            // 购物车不存在这个商品
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if (dishId != null){
                // 是菜品
                Dish dish = dishMapper.getById(dishId);
                setShopCartProperties(shoppingCart, dish);

            }
            if (setmealId != null){
                Setmeal setmeal = setmealMapper.getById(setmealId);
                setShopCartProperties(shoppingCart, setmeal);
            }
            shopCartMapper.insert(shoppingCart);
        }

    }

    private void setShopCartProperties(ShoppingCart shoppingCart, Dish dish) {
        shoppingCart.setName(dish.getName());
        shoppingCart.setImage(dish.getImage());
        shoppingCart.setAmount(dish.getPrice());// 单价
        shoppingCart.setDishFlavor(shoppingCart.getDishFlavor());
        shoppingCart.setDishId(shoppingCart.getDishId());
        shoppingCart.setSetmealId(shoppingCart.getSetmealId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(shoppingCart.getUserId());
        shoppingCart.setNumber(1);
    }
    private void setShopCartProperties(ShoppingCart shoppingCart, Setmeal setmeal) {
        shoppingCart.setName(setmeal.getName());
        shoppingCart.setImage(setmeal.getImage());
        shoppingCart.setAmount(setmeal.getPrice());// 单价
        shoppingCart.setDishFlavor(shoppingCart.getDishFlavor());
        shoppingCart.setDishId(shoppingCart.getDishId());
        shoppingCart.setSetmealId(shoppingCart.getSetmealId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(shoppingCart.getUserId());
        shoppingCart.setNumber(1);

    }
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shopCartMapper.list(userId);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shopCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 1、判断当前数量是否大于一
        ShoppingCart shoppingCart1 = shopCartMapper.getIsExist(shoppingCart);
        if (shoppingCart1.getNumber() > 1) {
            // 减一
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            shopCartMapper.updateNumber(shoppingCart1);
        }else {
            // 删除
            shopCartMapper.delete(shoppingCart);
        }
    }
}
